package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.compete;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.config.SzConfig;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysFiles;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompete;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysFilesService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCompeteService;
import com.xiaoxu.intranetweb.util.FileUploadUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Component
public class CompeteAdvanceBu {
    @Autowired
    private ITCompeteService iTCompeteService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ISysFilesService iSysFilesService;

    public SzResult competeAdvance(String id, String archivedStatus, String OpenMinBidStatus,String priceShowStatus,String token,String plantId) throws Exception {
        QueryWrapper<TCompete> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("DEL_FLAG", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        TCompete one = iTCompeteService.getOne(queryWrapper);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        if (one == null) {
            return SzResult.error("该标不存在");
        }
        if (archivedStatus != null && !archivedStatus.isEmpty()) {
            if (archivedStatus.equals("0")) {
                one.setArchived(0);
                sysLogBu.logRecord("是否归档", user.getTrueName(), "修改了标题为" + one.getTitle() + "的标的【是否归档】为【否】",plantId);
            } else {
                one.setArchived(1);
                sysLogBu.logRecord("是否归档", user.getTrueName(), "修改了标题为" + one.getTitle() + "的标的【是否归档】为【是】",plantId);
            }
        }
        if (OpenMinBidStatus != null && !OpenMinBidStatus.isEmpty()) {
            if (OpenMinBidStatus.equals("0")) {
                one.setOpenMinBid(0);
                sysLogBu.logRecord("是否开启最小投标量", user.getTrueName(), "修改了标题为" + one.getTitle() + "的标的【是否开启最小投标量】为【否】",plantId);
            } else {
                one.setOpenMinBid(1);
                sysLogBu.logRecord("是否开启最小投标量", user.getTrueName(), "修改了标题为" + one.getTitle() + "的标的【是否开启最小投标量】为【是】",plantId);
            }
        }
        if (priceShowStatus != null && !priceShowStatus.isEmpty()) {
            if (priceShowStatus.equals("0")) {
                one.setPriceShow(0);
                sysLogBu.logRecord("是否显示价格", user.getTrueName(), "修改了标题为" + one.getTitle() + "的标的【是否显示价格】为【否】",plantId);
            } else {
                one.setPriceShow(1);
                sysLogBu.logRecord("是否显示价格", user.getTrueName(), "修改了标题为" + one.getTitle() + "的标的【是否显示价格】为【是】",plantId);
            }
        }
        iTCompeteService.updateById(one);
        return SzResult.Success();
    }

    /**
     * 上传文件
     *
     */
    public SzResult uploadFile(MultipartFile file, String connId, String token) throws IOException {
        //存储文件返回绝对路径
        String filePath = FileUploadUtils.upload(SzConfig.getUploadPath(),file);
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        SysFiles sysFiles = new SysFiles();
        sysFiles.setConnId(connId);
        sysFiles.setUploadFrom(StpUtil.getLoginIdByToken(token).toString());
        sysFiles.setFileName(fileName);
        sysFiles.setFilePath(filePath);
        sysFiles.setFileSize(String.valueOf(file.getSize()));
        sysFiles.setFileType(suffix.replace(".", ""));
        sysFiles.setUploadTime(LocalDateTime.now());
        iSysFilesService.save(sysFiles);
        return SzResult.success(sysFiles);
    }


    //    下载文件
    public SysFiles downloadFile(String id) {
//        根据id区数据库中查询文件信息
        QueryWrapper<SysFiles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        SysFiles sysFiles = iSysFilesService.getOne(queryWrapper);
        return sysFiles;
    }


    //    删除文件
    public  SzResult deleteFile(String id) {
        QueryWrapper<SysFiles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        SysFiles sysFiles = iSysFilesService.getOne(queryWrapper);
        String filePath = sysFiles.getFilePath();
        String fileType = sysFiles.getFileType();
        File file = new File(filePath +sysFiles.getId()+"."+fileType);
        if (file.exists()) {
            file.delete();
        }
        iSysFilesService.removeById(id);
        return SzResult.Success();
    }
    //    文件预览
    public Path previewFile(String id) {
        QueryWrapper<SysFiles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        SysFiles sysFiles = iSysFilesService.getOne(queryWrapper);
        //        判断一下sysFiles.getFileType()是否是图片格式
        if (sysFiles.getFileType().equals("jpg") || sysFiles.getFileType().equals("png") || sysFiles.getFileType().equals("gif") || sysFiles.getFileType().equals("jpeg") || sysFiles.getFileType().equals("bmp") || sysFiles.getFileType().equals("webp") || sysFiles.getFileType().equals("svg")){
            String filePath = sysFiles.getFilePath();
            String fileType = sysFiles.getFileType();
            Path path = Paths.get(filePath +sysFiles.getId()+"."+fileType);
            return path;
        } else {
            return null;
        }
    }

    /**
     * 获取文件
     */
    public SzResult getFile() {
        QueryWrapper<SysFiles> queryWrapper = new QueryWrapper<>();;
        List<SysFiles> list = iSysFilesService.list(queryWrapper);
        return SzResult.success(list);
    }
}
